package com.gitee.huxing.algorithmic.practice;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description:
 * @ProjectName: AlgorithmicPactice
 * @Package: com.gitee.huxing.algorithmic.practice
 * @ClassName: CheckArithmeticSubarrays
 * @Author: huxing
 * @DateTime: 2022-03-16 3:54 PM
 */
public class CheckArithmeticSubarrays {

    public static void main(String[] args) {
        int[] nums = new int[]{4,6,5,9,3,7};
        int[] l = new int[]{0,0,2};
        int[] r = new int[]{2,3,5};
        System.out.println(checkArithmeticSubarrays(nums, l, r).toString());
    }

    public static List<Boolean> checkArithmeticSubarrays(int[] nums, int[] l, int[] r){
        List<Boolean> res = new ArrayList<Boolean>();
        int left = 0, right = 0;
        while(left < l.length && right < r.length){
            int len = r[right] - l[left] + 1;
            int[] ans = new int[len];
            for(int i = 0; i < len; i++){
                ans[i] = nums[i + left];
            }
            res.add(isArithmeticSubarrays(ans));
            left++;
            right++;
        }
        return res;
    }

    // 判断是否等差数列
    public static Boolean isArithmeticSubarrays(int[] nums){
        if (nums == null || nums.length <= 1) return false;
        Arrays.sort(nums);
        int step = nums[1] - nums[0];
        for(int i = 0; i < nums.length - 1; i++){
            if ((nums[i+1] - nums[i]) != step){
                return false;
            }
        }
        return true;
    }
}
